package com.ljm.article_comment.controller;


import com.ljm.article_comment.entity.User;
import com.ljm.article_comment.service.CommentService;
import com.ljm.article_comment.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

@Controller
public class LoginController {
    @Autowired
    private UserService userService;
    @Autowired
    private CommentService commentService;
    @GetMapping("/index")
    private String index(){
        return "index";
    }
    @PostMapping("/login")
    private String login(User user, Model model, HttpServletRequest request){
        User u=userService.login(user);
        System.out.println(u);
        if (u!=null){
            model.addAttribute("user",u);
            model.addAttribute("state",commentService.list());
            HttpSession session=request.getSession();
            session.setAttribute("user",u);
            return "list";
        }
        return "loser";
    }
}
